package com.tamo.ppresta.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gyf.immersionbar.ImmersionBar;
import com.hjq.http.EasyHttp;
import com.hjq.http.body.JsonBody;
import com.hjq.http.listener.OnHttpListener;
import com.tamo.ppresta.R;
import com.tamo.ppresta.base.BaseActivity;
import com.tamo.ppresta.base.BaseApplication;
import com.tamo.ppresta.bean.SendMessageBean;
import com.tamo.ppresta.constant.NET_CONSTANT;
import com.tamo.ppresta.http.HttpData;
import com.tamo.ppresta.httpapi.SendMessageApi;
import com.tamo.ppresta.json.ObjectToJsonUtil;
import com.tamo.ppresta.util.AESUtils;
import com.tamo.ppresta.util.ButtonUtils;
import com.tamo.ppresta.util.LogUtils;
import com.tamo.ppresta.util.MapUtils;
import com.tamo.ppresta.util.MyUtils;
import com.tamo.ppresta.util.ToastUtil;

import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author yeyang
 * @name ppresta
 * @class name：com.tamo.ppresta.activity
 * @class describe
 * @time 2022/12/19 13:52
 * @change
 * @chang time
 * @class describe
 */
public class SendMessageActivity extends BaseActivity {
    @BindView(R.id.iv_login_phone1)
    ImageView mIvLoginPhone1;
    @BindView(R.id.tv_country_number)
    TextView mTvCountryNumber;
    @BindView(R.id.rl_phone_num)
    RelativeLayout mRlPhoneNum;
    @BindView(R.id.iv_xieyi)
    ImageView mIvXieyi;
    @BindView(R.id.tv_xieyi)
    TextView mTvXieyi;
    @BindView(R.id.tv_continuar)
    TextView mTvContinuar;
    @BindView(R.id.tv_sms_tip)
    TextView mTvSmsTip;
    @BindView(R.id.iv_title_right)
    ImageView mIvTitleRight;
    @BindView(R.id.rl_title_right)
    RelativeLayout mRlTitleRight;
    @BindView(R.id.iv_title_back)
    ImageView mIvTitleBack;
    @BindView(R.id.rl_title_back)
    RelativeLayout mRlTitleBack;
    @BindView(R.id.tv_title_name)
    TextView mTvTitleName;

    @Override
    protected int setLayout() {
        return R.layout.activity_login;
    }

    @Override
    protected void setView() {
        mRlTitleBack.setOnClickListener(this::onClick);
        mTvContinuar.setOnClickListener(this::onClick);
    }

    @Override
    protected void setData() {

    }

    @Override
    public void onClick(View v) {
        ButtonUtils.isFastDoubleClick();
        switch (v.getId()) {
            case R.id.rl_title_back:
                finish();
                break;
            case R.id.tv_continuar:
                ToastUtil.showShort(this, "注册");
                sendMessage();
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        MyUtils.setStatusBarVisible(this);
        ImmersionBar.with(this)
                .statusBarColor(R.color.white)
                .transparentNavigationBar()  //透明导航栏，不写默认黑色(设置此方法，fullScreen()方法自动为true)
                .statusBarDarkFont(true)
                .init();
        ButterKnife.bind(this);
    }

    private void sendMessage() {
        String privateKey = AESUtils.getPrivateKey(24);
        String privateKeyFront = AESUtils.getPrivateKeyFront(privateKey);
        String privateKeyBehind = AESUtils.getPrivateKeyBehind(privateKey);
        LogUtils.info(TAG, "随机秘钥：" + privateKey);
        LogUtils.info(TAG, "随机秘钥19：" + privateKeyFront);
        LogUtils.info(TAG, "随机秘钥5：" + privateKeyBehind);
        BaseApplication.getInstance().setEasyHttpHead(NET_CONSTANT.HEADER_SECRETKEY_NAME, privateKeyFront);
        SendMessageApi sendMessageApi = new SendMessageApi().setLanguageCode(NET_CONSTANT.LANGUAGE_CODE)
                .setItemCode(NET_CONSTANT.ITEM_CODE)
                .setMobile("573101231231")
                .setSmsType("1");

        Map<String, Object> stringObjectMap = MapUtils.objectToMap(sendMessageApi);
        String json = ObjectToJsonUtil.getJson(stringObjectMap);
        LogUtils.info(TAG, json);
        String newJson = ObjectToJsonUtil.getNewJson(stringObjectMap);
        LogUtils.info(TAG, newJson);
        String aesEncrypt = AESUtils.aesEncrypt(newJson, privateKey, NET_CONSTANT.VIPARR);
        LogUtils.info(TAG, "加密： " + aesEncrypt);
        String aesDecrypt = AESUtils.aesDecrypt(aesEncrypt, privateKey, NET_CONSTANT.VIPARR);
        LogUtils.info(TAG, "解密： " + aesDecrypt);

        EasyHttp.post(this).api(new SendMessageApi()).body(new JsonBody(aesEncrypt + privateKeyBehind)).request(new OnHttpListener<HttpData<SendMessageBean>>() {
            @Override
            public void onSucceed(HttpData<SendMessageBean> result) {
                LogUtils.info(TAG, "onSucceed: " + result);
                startActivity(new Intent(SendMessageActivity.this, VerificationCodeActivity.class));
            }

            @Override
            public void onFail(Exception e) {
                LogUtils.info(TAG, "onFail: " + e.toString());
            }
        });
    }
}